<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="form" :model="formDate" :rules="formRules" label-width="120px">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="formDate.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
            <!-- 工号 入职时间 -->
            <el-col :span="12">
              <el-form-item label-width="80px" label="工号" prop="workNumber">
                <el-input v-model="formDate.workNumber" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <!--手机 聘用形式  -->
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="formDate.mobile"
                  size="mini"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label-width="80px" label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <select-tree :value="formDate.departmentId" @input="formDate.departmentId=$event" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="formDate.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label-width="80px" label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="formDate.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="formDate.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <!-- 员工照片 -->
              <el-form-item label-width="80px" label="员工头像">
                <!-- 放置上传图片 -->
                <image-upload v-model="formDate.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>

          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12">
              <el-form-item label-width="120px">
                <el-button size="mini" type="primary" @click="btnOk">保存更新</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { addEmployeeApi, editEmployeeApi, infoEmployeeApi } from '@/api/employee'
import selectTree from './components/select-tree.vue'
import ImageUpload from './components/imageUpload.vue'

export default {
  name: 'Detail',
  components: { selectTree, ImageUpload },
  data() {
    return {
      formDate: {
        'username': '',
        'mobile': '',
        'formOfEmployment': '',
        'workNumber': '',
        'departmentId': '',
        'timeOfEntry': '',
        'correctionTime': '',
        'staffPhoto': ''
      },
      formRules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }, {
          min: 1, max: 4, message: '姓名为1-4位'
        }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }, {
        //   pattern 正则表达式
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '请选择聘用形式', trigger: 'change' }],
        departmentId: [{ required: true, message: '请选择部门', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' }, {
          validator: (rule, value, callback) => {
            // 没有转正时间,校验通过
            if (!this.formDate.correctionTime) callback()
            // 有转正时间, 校验转正时间是否晚于转正时间
            new Date(value) > new Date(this.formDate.correctionTime) ? callback(new Error('转正时间不能小于入职时间')) : callback()
          }
        }],
        correctionTime: [{ required: true, message: '请选择转正时间', trigger: 'blur' }, {
          validator: (rule, value, callback) => {
            // 没有入职时间,校验通过
            if (!this.formDate.timeOfEntry) callback()
            // 有入职时间, 校验转正时间是否晚于入职时间
            new Date(this.formDate.timeOfEntry) > new Date(value) ? callback(new Error('转正时间不能小于入职时间')) : callback()
          }
        }]
      }
    }
  },
  created() {
    // 判断是否有动态路由，如果有则说明是修改，获取数据，回显
    if (this.$route.params && this.$route.params.id) this.getDetailData(this.$route.params.id)
  },
  methods: {
    // 提交
    async btnOk() {
      // 表单校验
      // 根据表单是否有ID判断发送添加还是修改请求
      this.formDate.id ? await editEmployeeApi(this.formDate) : await addEmployeeApi(this.formDate)
      // 提示成功
      this.$message.success('添加成功')
      // 返回
      this.$router.back()
    },
    // 编辑回显数据获取
    async getDetailData(id) {
      // 发送请求获取数据
      const res = await infoEmployeeApi(id)
      // 赋值变量
      this.formDate = res
    }
  }
}
</script>

<style scoped lang="scss">
.edit-form {
    background: #fff;
    padding: 20px;
    .inputW {
    width: 380px
    }
}
</style>
